<!-- 商品评价编辑弹窗 -->
<template>
  <div class="ele-body">
    <a-card :title="evaluationModalApp.title" :bordered="false">
      <a-form
        ref="form2"
        :model="evaluationModalApp.evaluation"
        :rules="evaluationModalApp.rules"
        :label-col="{md: {span: 7}, sm: {span: 24}}"
        :wrapper-col="{md: {span: 17}, sm: {span: 24}}">
        <a-row :gutter="16">
          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="评分:" name="score">
            </a-form-item>
          </a-col>
          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="评价内容:" name="content">
              <a-input
                v-model:value="evaluationModalApp.evaluation.content"
                placeholder="请输入评价内容"
                :maxlength="255"
                allow-clear
              />
            </a-form-item>
          </a-col>
          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="评价时间:" name="date">
              <a-date-picker
                v-model:value="evaluationModalApp.evaluation.date"
                placeholder="请选择评价时间"
                value-format="YYYY-MM-DD H:m:s"
                class="ele-fluid"/>
            </a-form-item>
          </a-col>
          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="用户:" name="userId">
              <a-input
                v-model:value="evaluationModalApp.evaluation.userId"
                placeholder="请输入用户"
                :maxlength="255"
                allow-clear
              />
            </a-form-item>
          </a-col>

          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="追评:" name="evaluationsId">
              <m-entity-select
                :default-value="evaluationModalApp.evaluation.evaluationsName"
                v-model:value="evaluationModalApp.evaluation.evaluationsId"
                placeholder="请选择追评"
                module="good"
                entity="Evaluation"
                class="ele-fluid"
              />
            </a-form-item>
          </a-col>
          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="商品:" name="commodityId">
              <m-entity-select
                :default-value="evaluationModalApp.evaluation.commodityName"
                v-model:value="evaluationModalApp.evaluation.commodityId"
                placeholder="请选择商品"
                module="good"
                entity="Commodity"
                class="ele-fluid"
              />
            </a-form-item>
          </a-col>
          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="订单:" name="orderId">
              <m-entity-select
                :default-value="evaluationModalApp.evaluation.orderName"
                v-model:value="evaluationModalApp.evaluation.orderId"
                placeholder="请选择订单"
                module="good"
                entity="Order"
                class="ele-fluid"
              />
            </a-form-item>
          </a-col>

          <a-col :md="12" :sm="24" :xs="24">
            <a-form-item :wrapper-col="{md: {offset: 6}}" style="margin-bottom: -20px">
              <!--class="ele-pull-right"-->
              <div v-if="evaluationModalApp.display">
                <teleport to="#evaluationEditModalFooter">
                  <div>
                    <a-space size="middle">
                      <a-button @click="evaluationListApp.editModalShowing=false">取消
                      </a-button>
                      <a-button
                        type="primary"
                        @click="onSubmit(evaluationModalApp.evaluation)"
                        :loading="loading">提交
                      </a-button>
                      <!--<a-button v-if="!eid"
                                type="dashed"
                                @click="continueSubmit"
                                :loading="loading">继续提交
                      </a-button>-->
                    </a-space>
                  </div>
                </teleport>
              </div>
              <div v-if="!evaluationModalApp.display">
                <a-space size="middle">
                  <a-button @click="onBack()">返回列表</a-button>
                  <a-button
                    type="primary"
                    @click="onSubmit(evaluationModalApp.evaluation)"
                    :loading="loading">提交
                  </a-button>
                  <!--<a-button v-if="!eid"
                            type="dashed"
                            @click="continueSubmit"
                            :loading="loading">继续提交
                  </a-button>-->
                </a-space>
              </div>

            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

  </div>

</template>

<script>
import {defineComponent, inject, reactive, onMounted} from 'vue'
import {useRoute, useRouter} from "vue-router"
import {EvaluationService} from "@/views/mall/good/evaluation/evaluationService";
import {VXETable} from 'vxe-table'
import {useStore} from "vuex";
import regions from 'ele-admin-pro/packages/regions.js';


import MEntitySelect from "@/components/MEntity/entitySelect";

export default defineComponent({
  components: {
    MEntitySelect,
  },
  setup() {
    // 省市区数据
    const cityData = regions
    const route = useRoute();
    const router = useRouter();
    const store = useStore();
    let routeId = route.params.id;
    let evaluationListApp = inject('evaluationListApp', reactive({}));
    let evaluationGrid = inject('evaluationGrid', reactive({}))
    const evaluationModalApp = reactive({
      id: null,
      evaluation: {},
      isEdit: false,
      display: false
    });
    evaluationModalApp.rules = {
      score: [
        {
          required: true,
          type: 'string',
          message: '请输入评分',
          trigger: 'blur'
        }
      ],
      content: [
        {
          required: false,
          type: 'string',
          message: '请输入评价内容',
          trigger: 'blur'
        }
      ],
      date: [
        {
          required: false,
          type: 'string',
          message: '请输入评价时间',
          trigger: 'blur'
        }
      ],
      userName: [
        {
          required: false,
          type: 'string',
          message: '请输入用户',
          trigger: 'blur'
        }
      ],
      userId: [
        {
          required: false,
          type: 'string',
          message: '请输入用户',
          trigger: 'blur'
        }
      ],
      evaluationsId: [
        {
          required: false,
          type: 'number',
          message: '请输入追评',
          trigger: 'blur'
        }
      ],
      evaluationsName: [
        {
          required: false,
          type: 'string',
          message: '请输入追评',
          trigger: 'blur'
        }
      ],
      commodityId: [
        {
          required: false,
          type: 'number',
          message: '请输入商品',
          trigger: 'blur'
        }
      ],
      commodityName: [
        {
          required: false,
          type: 'string',
          message: '请输入商品',
          trigger: 'blur'
        }
      ],
      orderId: [
        {
          required: false,
          type: 'number',
          message: '请输入订单',
          trigger: 'blur'
        }
      ],
      orderName: [
        {
          required: false,
          type: 'string',
          message: '请输入订单',
          trigger: 'blur'
        }
      ],
    }

    onMounted(() => {
      //清理数据
      evaluationModalApp.evaluation = reactive({});
      if (routeId) {//路由
        evaluationModalApp.id = routeId;
        evaluationModalApp.display = false;
        findEvaluation(evaluationModalApp.id);
      } else if (evaluationListApp.editModalForEdit) {//修改弹窗
        evaluationModalApp.id = evaluationListApp.currentId;
        evaluationModalApp.display = true;
      } else if (!evaluationListApp.editModalForEdit && evaluationListApp.addModalForEdit) {//新增弹窗
        evaluationModalApp.display = true;
      } else {
        evaluationModalApp.title = "新增学生";
      }
      if (evaluationModalApp.id) findEvaluation(evaluationModalApp.id);
    })

    const findEvaluation = (id) => {
      EvaluationService.findEvaluationForView(id).then((res) => {
        evaluationModalApp.evaluation = res.data;
        //TODO:为编辑准备.
        evaluationModalApp.title = "编辑学生 " + evaluationModalApp.evaluation.name;
      }).catch(error => {
        VXETable.modal.message({content: `查找学生出错，原因是：${error.message}`, status: 'error'});
      })
    }

    /*TODO:提交 新增&编辑*/
    const onSubmit = (data) => {
      // debugger;
      //当为数组时用逗号连接
      if (data.eid) {
        //修改
        EvaluationService.updateEvaluation(data).then((res) => {
          console.log(res);
          VXETable.modal.message({content: '操作成功', status: 'success'});
          setTimeout(onBack, 3000);
        }).catch(error => {
          VXETable.modal.message({content: `系统错误，原因是：${error.message}`, status: 'error'});
        })
      } else {
        //新增
        EvaluationService.saveEvaluation(data).then((res) => {
          console.log(res);
          VXETable.modal.message({content: '操作成功', status: 'success'});
          setTimeout(onBack, 3000);
        }).catch(error => {
          VXETable.modal.message({content: `系统错误，原因是：${error.message}`, status: 'error'});
        })
      }
    }

    /*TODO:继续提交*/
    const continueSubmit = () => {
      VXETable.modal.message({content: 'error 提示', status: 'error'});
    }

    /*TODO:返回列表*/
    const onBack = () => {
      router.push(`/mall/good/evaluation`);
      store.dispatch('user/tabRemove', route.fullPath);
      setTimeout(() => {
        evaluationGrid.value.commitProxy("query");
      }, 1500);
      evaluationListApp.editModalShowing = false;
    }
    return {
      cityData,
      routeId,
      evaluationListApp,
      evaluationModalApp,
      onSubmit,
      continueSubmit,
      onBack,
    }

  },
})
</script>

<style scoped>

</style>
